import { createSlice, PayloadAction } from '@reduxjs/toolkit';
import { fetchAuthorization } from './auth.actions';

const initialState = {
	user: {
		token: '',
		data: { email: '', password: '', fullName: '', id: '' }
	},
	loading: false,
	error: null,
	success: false
};

export const userAuthorizationSlice = createSlice({
	name: 'userAuthorization',
	initialState,
	reducers: {},
	extraReducers: (builder) => {
		builder.addCase(fetchAuthorization.pending, (state) => {
			state.loading = true;
			state.error = null;
		}),
		builder.addCase(fetchAuthorization.fulfilled, (state, action) => {
			console.log('data from back on authirozation', action.payload);
			state.user = action.payload;
			state.success = true;
		}),
		builder.addCase(fetchAuthorization.rejected, (state, action) => {
			console.log('ошибка из слайса авторизации', action.payload);
			state.error = action.error.message;
		});
	}
});

export default userAuthorizationSlice.reducer;
